<?php

namespace App\Crontab;
class ZbtSearchCrontab
{
    public function execute()
    {
        logger()->info('----------开始:ZBT采集------' . date('Y-m-d H:i:s'));
        $page = 1;
        //产品所有zbt id
        $settingCofig = \App\Setting\Model\SettingConfig::query()
            ->whereIn('key', ['zbt_premium_ratio', 'zbt_rmb_ratio'])
            ->pluck('value', 'key');
        //溢价,人民币比率
        $settingConfigPremium = $settingCofig['zbt_premium_ratio']; //溢价
        $settingConfigRmb = $settingCofig['zbt_rmb_ratio'];  //人民币
        do {
            $itemIdsArr = \App\Config\Model\ConfigProduct::query()
                ->where('source_type',\App\Config\Model\ConfigProduct::SOURCE_TYPE_CSGO)
                ->pluck('item_id')->toArray();
            $res = \App\Lib\Library\ZbtQuery::queryGoodsSearch($page);
            $addData = [];
            //需要新增
            foreach ($res as $key => $val) {
                if (!in_array($key, $itemIdsArr)) {
                    $addData[] = [
                        'source_type' => \App\Config\Model\ConfigProduct::SOURCE_TYPE_CSGO,
                        'price' => number_format($val['priceInfo']['price'] * $settingConfigPremium, 2, '.', '') * \App\Lib\AdminEnum::PRICE_MULTIPLE, //溢价
                        'origin_price' => $val['priceInfo']['price'] * \App\Lib\AdminEnum::PRICE_MULTIPLE,      //原价
                        'rmb_price' => number_format($val['priceInfo']['price'] * $settingConfigPremium * $settingConfigRmb, 2, '.', '') * \App\Lib\AdminEnum::PRICE_MULTIPLE, //溢价,
                        'name' => $val['itemName'],
                        'origin_name' => $val['shortName'],
                        'english_name' => $val['marketHashName'],
                        'cover' => $val['imageUrl'],
                        'stock' => empty($val['priceInfo']['autoDeliverQuantity']) ? 0 : $val['priceInfo']['autoDeliverQuantity'], //自动发货库存
                        'tag_exterior' => $val['rarityColor'],
                        'category_name' => $val['typeName'],
                        'grade' => $val['rarityName'],
                        'extend_field' => json_encode([
                            'type' => $val['type'],
                            'rarity' => ['rarity_common'],
                            'quality' => $val['quality'],
                            'exterior' => $val['exterior'],
                            'exteriorName' => $val['exteriorName'],
                        ]),
                        'item_id' => $key,
                        'create_time' => time(),
                        'update_time' => time()
                    ];
                }
            }
            if (!empty($addData)) {
                \App\Config\Model\ConfigProduct::query()->insert($addData);
            }
            $page++;
        } while (!empty($res));
        logger()->info('----------结束:ZBT采集------' . date('Y-m-d H:i:s'));
    }
}